﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Xml.Linq;
using Windows.Infrastructure.Share.Security;
using Windows.Admin.Infrastructure;

namespace Windows.Admin.Service
{
    [Authorize]
    public class InformHub : Hub
    {
        private readonly ICurrentUser _currentUser;
        public InformHub(ICurrentUser currentUser)
        {
            _currentUser = currentUser;
        }
        public override async Task OnConnectedAsync()
        {
            await base.OnConnectedAsync();
            long userId = Context.User.FindUserId().Value;
            if (Common.LinkInfos.Keys.Contains(userId))
            {
                Common.LinkInfos[userId] = Context.ConnectionId;
            }
            else
            {
                Common.LinkInfos.TryAdd(userId, Context.ConnectionId);
            }
        }
        public override async Task OnDisconnectedAsync(Exception? exception)
        {
            await base.OnDisconnectedAsync(exception);
            long userId = Context.User.FindUserId().Value;
            if (Common.LinkInfos.Keys.Contains(userId))
            {
                Common.LinkInfos.TryRemove(userId, out string value);
            }
        }
    }
}
